package com.herench.bs.handler;

import com.herench.bs.annotation.Handler;
import com.herench.bs.core.TokenProvider;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.support.TokenLifeCycle;
import com.herench.bs.utils.DataFormatUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * 刷新令牌
 * Created by ThinkPad on 2017/8/25.
 */
@Component
@Handler(code = "100109",name="刷新令牌")
public class RefreshTokenHandler extends AbstractMessageHandler {

    @Override
    protected ResponseMessage innerHandler(final RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = null;
        String tokenId = (String) requestMessage.getArgs().get("tokenId");
        Assert.isTrue(StringUtils.hasLength(tokenId), "tokenId 不能为空.");
        TokenLifeCycle tokenLifeCycle = TokenProvider.refreshTokenLife(tokenId);
        responseMessage = new ResponseMessage(requestMessage.getId());
        responseMessage.setCode("0");
        responseMessage.setMessage("操作成功");
        responseMessage.setDataAttribute("expires", DataFormatUtils.getTimeString(tokenLifeCycle.getExpression()));
        return responseMessage;
    }
}
